--    Program: 		CAPPOIMP_ATTACHMENT_DOCUMENT_TL.sql
--    Description:  Import PO Attached Document information 
--    Revision:     1st     
--    Parameter: 	
--    Created By:   Francis Lee
--    Date:         27-Jan-2006

set doc off;      
set verify off;
set serveroutput on size 1000000;
whenever sqlerror exit failure rollback;

declare   
   l_processed_count		number;
   l_rejected_count 		number;
   l_err_msg				varchar2(2000);
   l_err_msg_tmp			varchar2(2000);
   
   l_document_id  			number;
                    		                 		
   cursor cur_attachment is
      select * from CRST_FND_DOCUMENTS_TL;

begin
   l_processed_count := 0;
   l_rejected_count  := 0;
   l_err_msg := '';
   
   for cur in cur_attachment loop  
   	 l_err_msg_tmp := '';
   	 
     begin
   	   select document_id_n 
   	   into   l_document_id
       from   crst_fnd_documents
       where  document_id = cur.document_ID;
	 exception
	 	 when others then
	 	   l_err_msg_tmp := 'New DOCUMENT_ID not found';		
	 end;	
	 
     begin
       -- import data
       insert into FND_DOCUMENTS_TL
       (
         DOCUMENT_ID             ,
         CREATION_DATE           ,
         CREATED_BY              ,
         LAST_UPDATE_DATE        ,
         LAST_UPDATED_BY         ,
         LAST_UPDATE_LOGIN       ,
         LANGUAGE                ,
         DESCRIPTION             ,
         SHORT_TEXT              ,
         FILE_NAME               ,
         MEDIA_ID                ,
         REQUEST_ID              ,
         PROGRAM_APPLICATION_ID  ,
         PROGRAM_ID              ,
         PROGRAM_UPDATE_DATE     ,
         DOC_ATTRIBUTE_CATEGORY  ,
         DOC_ATTRIBUTE1          ,
         DOC_ATTRIBUTE2          ,
         DOC_ATTRIBUTE3          ,
         DOC_ATTRIBUTE4          ,
         DOC_ATTRIBUTE5          ,
         DOC_ATTRIBUTE6          ,
         DOC_ATTRIBUTE7          ,
         DOC_ATTRIBUTE8          ,
         DOC_ATTRIBUTE9          ,
         DOC_ATTRIBUTE10         ,
         DOC_ATTRIBUTE11         ,
         DOC_ATTRIBUTE12         ,
         DOC_ATTRIBUTE13         ,
         DOC_ATTRIBUTE14         ,
         DOC_ATTRIBUTE15         ,
         APP_SOURCE_VERSION      ,
         SOURCE_LANG   
       )
       values
       (
         l_document_id                 ,
         cur.CREATION_DATE             ,
         CAPBRIMP_MAPPING.GetUserMapping(cur.CREATED_BY),
         cur.LAST_UPDATE_DATE          ,
         CUR.LAST_UPDATED_BY,
         cur.LAST_UPDATE_LOGIN         ,
         cur.LANGUAGE                  ,
         cur.DESCRIPTION               ,
         cur.SHORT_TEXT                ,
         cur.FILE_NAME                 ,
         cur.MEDIA_ID                  ,
         cur.REQUEST_ID                ,
         cur.PROGRAM_APPLICATION_ID    ,
         cur.PROGRAM_ID                ,
         cur.PROGRAM_UPDATE_DATE       ,
         cur.DOC_ATTRIBUTE_CATEGORY    ,
         cur.DOC_ATTRIBUTE1            ,
         cur.DOC_ATTRIBUTE2            ,
         cur.DOC_ATTRIBUTE3            ,
         cur.DOC_ATTRIBUTE4            ,
         cur.DOC_ATTRIBUTE5            ,
         cur.DOC_ATTRIBUTE6            ,
         cur.DOC_ATTRIBUTE7            ,
         cur.DOC_ATTRIBUTE8            ,
         cur.DOC_ATTRIBUTE9            ,
         cur.DOC_ATTRIBUTE10           ,
         cur.DOC_ATTRIBUTE11           ,
         cur.DOC_ATTRIBUTE12           ,
         cur.DOC_ATTRIBUTE13           ,
         cur.DOC_ATTRIBUTE14           ,
         cur.DOC_ATTRIBUTE15           ,
         cur.APP_SOURCE_VERSION        ,
         cur.SOURCE_LANG   
       );
     exception
       when others then
     	 l_err_msg_tmp := sqlerrm;
     end;
     
	 if l_err_msg_tmp IS NULL then
       l_processed_count := l_processed_count + 1;  
     else
       l_rejected_count := l_rejected_count + 1;
       l_err_msg := l_err_msg_tmp; 
     end if;  
   end loop;

   if l_rejected_count = 0 then
   	 commit;
   else
     rollback;
   end if;
   
   -- insert into log table
   insert into capcnv_log ( prog_id,process_date, record_processed,record_rejected,record_extracted,err_msg) 
    values ('CAPPOIMP_ATTACHMENT_DOCUMENT_TL', sysdate, l_processed_count, l_rejected_count, l_processed_count-l_rejected_count, l_err_msg );
   
   commit;
exception 
  when others then
    dbms_output.put_line(sqlerrm);
    rollback;  
end;
/


--exit
